﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;

namespace DelonixRegiaHotel
{
    public partial class CreateCreditCard : Form
    {
        public CreateCreditCard()
        {
            InitializeComponent();
            ArrayList getCustomerInfo = ManageCustomerDBManager.GetAllCustomerDetails();
            cboxnric.Items.Add("-Select Nric-");
            foreach (Customer c in getCustomerInfo)
            {
                cboxnric.Items.Add(c.Nric + ", " + c.CustomerID);
            }
            cboxnric.SelectedIndex = 0;
        }

        private void cboxnric_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cboxnric.SelectedIndex == 0)
            {
                lblshowname.Text = "-";
                lblshowemail.Text = "-";
                lblshowcontactno.Text = "-";
                lblshowcreditcardholdername.Text = "-";
                lblshowcustomerid.Text = "-";
            }

            ArrayList getCustomerInfo = ManageCustomerDBManager.GetAllCustomerDetails();
            foreach (Customer c in getCustomerInfo)
            {
                if (cboxnric.SelectedItem.ToString().Contains(c.CustomerID) && cboxnric.SelectedItem.ToString().Contains(c.Nric))
                {
                    lblshowname.Text = c.FirstName + " " + c.LastName;
                    lblshowemail.Text = c.Email;
                    lblshowcontactno.Text = Convert.ToString(c.ContactNo);
                    lblshowcreditcardholdername.Text = c.FirstName + " " + c.LastName;
                    lblshowcustomerid.Text = c.CustomerID;
                    break;
                }
            }
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btncreate_Click(object sender, EventArgs e)
        {
            if(tbxcreditcardexpdate.Text.Equals("") || tbxcreditcardnumber.Text.Equals("") || cboxnric.SelectedIndex == 0)
            {
                MessageBox.Show("Please fill in all the information");
                return;
            }

            if(ManageCustomerDBManager.TestWhetherCardNumberExists(Convert.ToInt64(tbxcreditcardnumber.Text)))
            {
                MessageBox.Show("Please check credit card number");
                return;
            }

            DateTime dt;
            if (!DateTime.TryParse(tbxcreditcardexpdate.Text, out dt))
            {
                MessageBox.Show("Invalid Date, please check credit card date entered");
                return;
            }

            CreditCardInfo newCreditCardInfo = new CreditCardInfo();
            newCreditCardInfo.CreditCardHolder = lblshowcreditcardholdername.Text;
            newCreditCardInfo.CustomerID = lblshowcustomerid.Text;
            newCreditCardInfo.CreditCardNo = Convert.ToInt64(tbxcreditcardnumber.Text);
            newCreditCardInfo.CreditCardExpDate = Convert.ToDateTime(tbxcreditcardexpdate.Text);
            if (MessageBox.Show("Do you want to create new credit card info?", "Confirm Create?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (ManageCustomerDBManager.CreateCreditCardInfo(newCreditCardInfo) == 1)
                {
                    MessageBox.Show("New Credit Card Created");
                    this.Hide();
                }
            }
        }

        private void tbxCheckAllForNumbers_TextChanged(object sender, EventArgs e) // int only generalized method
        {
            Exception X = new Exception();
            TextBox T = (TextBox)sender;

            try
            {
                if (T.Text != "-")
                {
                    Int64 x = Int64.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }




    }
}
